package vn.personal.anhht.bloback12.activity;

import vn.personal.anhht.bloback12.database.CallBlockDatabaseAdapter;
import vn.personal.anhht.bloback12.util.Constants;
import vn.personal.anhht.bloback12.util.LocalizationUtil;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class TabPickCustomActivity extends Activity{

	private CallBlockDatabaseAdapter databaseAdapter;
	Resources rs;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		LocalizationUtil.setupLanguage(getApplicationContext());
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pick_contact_custom);
		rs = getResources();
		initFormAdd();
		Log.e("CUSTOM TAB", "CUSTOM TAB");
	}
	
	private Button btnAdd;
	private Button btnCancel;

	/*
	 * TyHH: Init form custom pick phone number
	 */
	public void initFormAdd() {
		databaseAdapter = new CallBlockDatabaseAdapter(getApplicationContext());
		btnAdd = (Button) findViewById(R.id.btn_pcustom_add);
		btnAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText etName = (EditText) findViewById(R.id.et_pcustom_name);
				EditText etPhone = (EditText) findViewById(R.id.et_pcustom_phone);
				String name = etName.getText().toString().trim();
				String phone = etPhone.getText().toString().trim();
				if(("".equals(name) && "".equals(phone)) || "".equals(phone)){
					Toast.makeText(getApplicationContext(), rs.getString(R.string.pc_input_empty), Toast.LENGTH_LONG).show();
				}
				else{

					if ("".equals(name)) {
						name = rs.getString(R.string.pick_string_noname);
					}				
					if ("".equals(phone)) {
						phone = rs.getString(R.string.pick_string_nophone);
					}					
					databaseAdapter.open();
					if(databaseAdapter.isPhoneExist(phone)){
						databaseAdapter.close();
						Toast.makeText(getApplicationContext(), rs.getString(R.string.pc_phone_exist), Toast.LENGTH_LONG).show();
					}else{
						databaseAdapter.addBlackList(name, phone, "", "", 0, 0, 0);
						databaseAdapter.close();
						getParent().setResult(Constants.RESPONSE_PICK_CUSTOM_OK);
						finish();
					}
				}
				
				
			}
		});
		btnCancel = (Button) findViewById(R.id.btn_pcustom_cancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getParent().setResult(Constants.RESPONSE_PICK_CUSTOM_CANCEL);
				finish();
			}
		});
	}
}
